package com.link.base.base.product.service;

import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.product.dao.mybatis.mapper.QuotePartsCostLineMapper;
import com.link.base.base.product.dao.mybatis.mapper.QuotePartsCostMapper;
import com.link.base.base.product.model.QuotePartsCost;
import com.link.base.base.product.model.QuotePartsCostLine;
import com.link.base.base.project.dao.mybatis.mapper.QuoteRoundMapper;
import com.link.base.base.project.model.QuoteRound;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.CheckUpdAndDelUtil;
import com.link.core.util.CoreYmlConstants;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuotePartsCostServiceImpl extends BasicServiceImpl<QuotePartsCost> implements QuotePartsCostService {

    @Resource
    private QuotePartsCostMapper quotePartsCostMapper;

    @Resource
    private QuotePartsCostLineMapper quotePartsCostLineMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private QuoteRoundMapper quoteRoundMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Override
    public BasicMapper<QuotePartsCost> getBasicMapper() {
        return quotePartsCostMapper;
    }


    @Transactional(rollbackFor = {BasicServiceException.class})
    public void deleteById(QuotePartsCost entity) throws Exception {
        this.beforDelete(entity);
        this.checkId(entity);
        // 删除行
        QuotePartsCostLine quotePartsCostLine = new QuotePartsCostLine();
        quotePartsCostLine.setHeadId(entity.getId());
        quotePartsCostLineMapper.deleteByHeadId(quotePartsCostLine);
        // 删除头
        Integer returnStatus = this.getBasicMapper().deleteById(entity);
        boolean checkDelete = CheckUpdAndDelUtil.checkUpdateOrDeleteVersion(returnStatus, entity, "delete");
        if (checkDelete && "true".equalsIgnoreCase(CoreYmlConstants.getCheckCorp())) {
            throw new BasicServiceException("当前记录已经被其他人修改或者您无权删除此记录，请查询数据后重试！");
        } else if (checkDelete && !"true".equalsIgnoreCase(CoreYmlConstants.getCheckCorp())) {
            throw new BasicServiceException("当前记录已经被其他人修改，请查询数据后重试！");
        } else {
            this.afterDelete(entity);
        }
    }

    @Override
    @Transactional
    public void addSelectPartCost(QuotePartsCost quotePartsCost) {
        CoreUser user = UserUtil.getUser();
        quotePartsCost.setId(keyGenerateDao.keyGenerate());
        quotePartsCost.setCreatedBy(user.getId());
        quotePartsCost.setLastUpdatedBy(user.getId());
        quotePartsCost.setOrgId(user.getOrgId());
        quotePartsCost.setPostnId(user.getPostnId());
        // 1、添加对外报价行：总成件明细
        quotePartsCostMapper.addSelectPartCost(quotePartsCost);
        // 2、添加对外报价：零部件报价明细
        quotePartsCostLineMapper.addBySelectPartCostId(quotePartsCost);

        /*// 2、清空对外报价重新计算
        QuoteRound quoteRound = new QuoteRound();
        quoteRound.setProjectHeadId(quotePartsCost.getProjectId());
        quoteRoundMapper.deleteByProjectId(quoteRound);*/

    }

    @Override
    public List<QuotePartsCost> queryPickListPage(QuotePartsCost entity) throws Exception {
        return quotePartsCostMapper.queryPickListPage(entity);
    }


    public void afterDelete(QuotePartsCost quotePartsCost) throws Exception {
        // 2、清空对外报价重新计算
        QuoteRound quoteRound = new QuoteRound();
        quoteRound.setProjectHeadId(quotePartsCost.getProjectId());
        quoteRoundMapper.deleteByProjectId(quoteRound);
    }


}
